//
//  CZComposePictureView.swift
//  weiBo
//
//  Created by apple on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZComposePictureView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
    
    //定义加号按钮的闭包
    var didSelectAddImageClosure: (()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        delegate = self
        dataSource = self
        //注册cell
        registerClass(CZComposePictureViewCell.self,forCellWithReuseIdentifier: "cell")
        self.backgroundColor = UIColor.redColor()
    
    }
    
    //设置cell的大小
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        获取布局对象
        let flowLayOut = self.collectionViewLayout as!  UICollectionViewFlowLayout
        let itemMargin: CGFloat = 5
        let itemWidth = (self.frame.size.width - 2 * itemMargin) / 3
        
        flowLayOut.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayOut.minimumInteritemSpacing = itemMargin
        flowLayOut.minimumLineSpacing = itemMargin
    
    }
    
    //懒加载图片数组
     var images: [UIImage] = [UIImage]()
    //添加图片
    func addImage(image: UIImage) {
        images.append(image)
        reloadData()
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = images.count
        if count == 0 || count == 9 {
            return count
        }else {
            return count + 1
        }
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CZComposePictureViewCell
        if indexPath.item < images.count {
            cell.image = images[indexPath.item]
        }else {
            cell.image = nil
        }
        
        cell.deleteButtonClosure = { [weak self] in
            if self?.images.count == 0 {
                self?.hidden = true
            }
            
            self?.images.removeAtIndex(indexPath.item)
            
            self?.reloadData()
        }
        
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if indexPath.item == images.count {
            //点击加号按钮
            didSelectAddImageClosure?()
        }
    }
    
    
}

class CZComposePictureViewCell: UICollectionViewCell {
    //删除闭包的图片
    var deleteButtonClosure: (() -> ())?
    
    //设置图片

    var image:UIImage? {
        didSet {
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add")
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteImageButton.hidden = true
            }else {
                imageView.image = image
                imageView.highlightedImage = nil
                deleteImageButton.hidden = false

            }
        }
    
    }
    
    //懒加载图片
   private lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    //懒加载删除图片按钮
    private lazy var deleteImageButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "deleteImageButtonClick", forControlEvents: .TouchUpInside)
        btn.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpUI() {
    
        contentView.addSubview(imageView)
        contentView.addSubview(deleteImageButton)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteImageButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
    }
    
    
    //删除按钮的点击事件
    @objc private func deleteImageButtonClick() {
    
        deleteButtonClosure?()
    
    }
    
    
}








